/*
 * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.butterfly.vv;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.beem.project.beem.R;
import com.butterfly.vv.utils.OnSeekToListenerImp;
import com.butterfly.vv.utils.SeekToMode;
import com.butterfly.vv.vv.utils.VVXMPPUtils;
import com.teleca.jamendo.api.Contact;
import com.teleca.jamendo.api.VVAlbum;
import com.teleca.jamendo.api.VVImage;
import com.teleca.jamendo.widget.RemoteImageView;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Central part of the UI. Touching cover fades in 4-way media buttons. 
 * 4-way media buttons fade out after certain amount of time. Other parts 
 * of layout are progress bar, total play time, played time, song title, 
 * artist name and jamendo slider.<br><br>
 * 
 * License information is implemented overlaying CreativeCommons logo over 
 * the album picture. Information about type of license is retrieved concurrently
 * to track bufferring.
 * 
 * @author Lukasz Wisniewski
 */
public class ShowSingleImageActivity extends Activity {



	

	// XML layout
	private ImageButton mNextImageButton;
	private ImageButton mPrevImageButton;
	private RemoteImageView mCoverImageView;

	private Animation mFadeInAnimation;
	private Animation mFadeOutAnimation;
	
	private String mBetterRes;	
	Handler mHandlerOfFadeOutAnimation; 
	Runnable mRunnableOfFadeOutAnimation; 
	
	private VVAlbum mVvAlbum;
	ArrayList<VVImage> vvImages;
	Contact vvmContact;
	int position;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("VV", "PlayerActivity.onCreate");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.myshowimage);
		
		
		if (getIntent().getAction().equals("com.btf.vv.IMAGES")) {
        	mVvAlbum = (VVAlbum) getIntent().getSerializableExtra("vvAlbum");
        	vvImages = (ArrayList<VVImage>)getIntent().getSerializableExtra("vvImages");
        	//Log.i("tag", "--"+mVvAlbum.getName());
        	position = getIntent().getIntExtra("position", 0);
        	vvmContact = (Contact)getIntent().getSerializableExtra("vvmContact");
        	for(VVImage vvImage : vvImages){
        		Log.i("tag", "ShowSingleImageActivity "+position+"--"+vvImage.getName()+" "+ vvmContact.getmID());
        	}
		}

		// XML binding
		//mBetterRes = getResources().getString(R.string.better_res);
		
		mCoverImageView = (RemoteImageView)findViewById(R.id.CoverImageView);
		mCoverImageView.setOnClickListener(mCoverOnClickListener);
		//mCoverImageView.setDefaultImage(R.drawable.no_cd_300);
		//mCoverImageView.setImageResource(R.drawable.no_cd_125);
		//mCoverImageView.setImageUrl("http://"+vvImages.get(position).getIp()+Constants.VVIMAGEPATH+ VVXMPPUtils.makeVVXmppJid(vvmContact.getmID()) +"/"+vvImages.get(position).getName());
		mCoverImageView.setImageUrl("http://"+Constants.XMPPSERVER+Constants.VVIMAGEPATH+ VVXMPPUtils.makeVVXmppJid(vvmContact.getmID()) +"/image/"+vvImages.get(position).getName());

		handleIntent();
		
		//used for Fade Out Animation handle control
		mHandlerOfFadeOutAnimation = new Handler(); 
		mRunnableOfFadeOutAnimation =new Runnable(){
			public void run() {				
				if ( mFadeInAnimation.hasEnded() )
					//20130131mPlayImageButton.startAnimation(mFadeOutAnimation);
					mNextImageButton.startAnimation(mFadeOutAnimation);
			}
			
		}; 

		mNextImageButton = (ImageButton) findViewById(R.id.NextImageButton);	
		mNextImageButton.setOnTouchListener(new OnSeekToListenerImp(
				this,mCoverImageView, SeekToMode.EForward,position,vvImages,vvmContact));
//		mNextImageButton.setOnTouchListener(new OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				if (event.getAction() == MotionEvent.ACTION_UP) {
//					mCoverImageView.setImageResource(R.drawable.avatar_default);
//				}
//				Log.i("VV", "mNextImageButton.setOnTouchListener");
//				
//				return false;
//			}
//		});

		mPrevImageButton = (ImageButton) findViewById(R.id.PrevImageButton);
		//2013mPrevImageButton.setOnTouchListener(mOnRewindTouchListener);
		mPrevImageButton.setOnTouchListener(new OnSeekToListenerImp(
				this,mCoverImageView,  SeekToMode.ERewind,position,vvImages,vvmContact));
//		mPrevImageButton.setOnTouchListener(new OnTouchListener() {
//			
//			@Override
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				if (event.getAction() == MotionEvent.ACTION_UP) {
//					mCoverImageView.setImageResource(R.drawable.no_cd_300);
//				}
//				return false;
//			}
//		});

		mFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.remoteimagefade_in);
		mFadeInAnimation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation animation) {
				mHandlerOfFadeOutAnimation
						.removeCallbacks(mRunnableOfFadeOutAnimation);
				mHandlerOfFadeOutAnimation.postDelayed(
						mRunnableOfFadeOutAnimation, 7500);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// nothing here
			}

			@Override
			public void onAnimationStart(Animation animation) {
				setMediaVisible();
			}

		});

		mFadeOutAnimation = AnimationUtils.loadAnimation(this, R.anim.remoteimagefade_out);
		mFadeOutAnimation.setAnimationListener(new AnimationListener(){

			@Override
			public void onAnimationEnd(Animation animation) {
				setMediaGone();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// nothing here
			}

			@Override
			public void onAnimationStart(Animation animation) {
				setFadeOutAnimation();
			}

		});
		
		
		//mCurrentAlbum = null;
		
	

		
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.i("VV", "PlayerActivity.onResume");

		
		//refresh shuffle and repeat icons
		if(true){
			
		}

	}
	
	
	public void doCloseActivity(){
		finish();	
		
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.i("VV", "PlayerActivity.onPause");

	}

	/**
	 * Makes 4-way media visible
	 */
	private void setMediaVisible(){
		mNextImageButton.setVisibility(View.VISIBLE);
		mPrevImageButton.setVisibility(View.VISIBLE);
	}

	/**
	 * Makes 4-way media gone
	 */
	private void setMediaGone(){
		mNextImageButton.setVisibility(View.GONE);
		mPrevImageButton.setVisibility(View.GONE);
	}

	/**
	 * Sets fade out animation to 4-way media
	 */
	private void setFadeOutAnimation(){
		mNextImageButton.setAnimation(mFadeOutAnimation);
		mPrevImageButton.setAnimation(mFadeOutAnimation);
	}

	/**
	 * Sets fade out animation to 4-way media
	 */
	private void setFadeInAnimation(){
		mNextImageButton.setAnimation(mFadeInAnimation);
		mPrevImageButton.setAnimation(mFadeInAnimation);
	}

	/**
	 * Launches fade in/out sequence
	 */
	private OnClickListener mCoverOnClickListener = new OnClickListener(){

		@Override
		public void onClick(View v) {

			if(mNextImageButton.getVisibility() == View.GONE||mPrevImageButton.getVisibility() == View.GONE)
			{
				setMediaVisible();
				setFadeInAnimation();
				//mPlayImageButton.startAnimation(mFadeInAnimation);
				mNextImageButton.startAnimation(mFadeInAnimation);
			}
		}

	};
	
	/**
	 * next button action
	 */
//	private OnSeekToListenerImp mOnForwardTouchListener = new OnSeekToListenerImp(
//			this,mCoverImageView, SeekToMode.EForward);
//	
//	/**
//	 * prev button action
//	 */	
//	private OnSeekToListenerImp mOnRewindTouchListener = new OnSeekToListenerImp(
//			this,mCoverImageView,  SeekToMode.ERewind);

	

	/**
	 * PlayerEngineListener implementation, manipulates UI
	 */
	/**
	 * Loads playlist to the PlayerEngine
	 * 
	 * @param playlist
	 */
	private void handleIntent(){
		Log.i("VV", "PlayerActivity.handleIntent");

	}
	


	/**
	 * This creates playlist based on url that was passed in the intent,
	 * e.g. http://www.jamendo.com/pl/track/325654 or http://www.jamendo.com/pl/album/7505
	 * 
	 *  @author Lukasz Wisniewski
	 */
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_MENU){
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void onStartSeekToProcess(){		
		mHandlerOfFadeOutAnimation
				.removeCallbacks(mRunnableOfFadeOutAnimation);
	}
	
	public void onFinishSeekToProcess(){		
		mHandlerOfFadeOutAnimation
				.removeCallbacks(mRunnableOfFadeOutAnimation);
		mHandlerOfFadeOutAnimation.postDelayed(
				mRunnableOfFadeOutAnimation, 7500);		
	}
}
